package com.jg.visitor.common.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jg.visitor.common.utils.DateTimeUtil;
import com.jg.visitor.controller.enums.VisitorApplyRecordStatusEnum;
import com.jg.visitor.domain.VisitorApplyRecord;
import com.jg.visitor.service.VisitorApplyRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author 小狐狸
 */
@Slf4j
@Component
public class ClearTimeoutOrderService {

    @Resource
    private VisitorApplyRecordService visitorApplyRecordService;

    public void clearTimeoutOrder(){
        String timeNow = DateTimeUtil.getTimeNow();

        QueryWrapper<VisitorApplyRecord> query = new QueryWrapper<VisitorApplyRecord>()
                //当前时间已经超过了结束时间
                .apply(" convert(datetime,end_time) < convert(datetime,'" + timeNow + "')")
                //状态在等待审批
                .in("status",
                        Arrays.asList(VisitorApplyRecordStatusEnum.WAIT_RESPONDENTS_APPROVE.getStatusCode(),
                        VisitorApplyRecordStatusEnum.WAIT_SAFER_APPROVE.getStatusCode()))
                .select("id")
               ;


        List<VisitorApplyRecord> timeoutOrder = visitorApplyRecordService.list(query);

        //设置状态为超期未处理
        if(!CollectionUtils.isEmpty(timeoutOrder)){
            timeoutOrder.forEach(order -> {
                order.setStatus(VisitorApplyRecordStatusEnum.APPROVE_TIME_OUT.getStatusCode());
                order.setGmtModified(DateTimeUtil.getTimeNow());
            });
            visitorApplyRecordService.updateBatchById(timeoutOrder);
            log.info("timeoutOrder======>{}",timeoutOrder);
        }


    }

}
